from flask.ext.wtf import Form
from wtforms import BooleanField, TextField, TextAreaField, validators, StringField, HiddenField
from wtforms.validators import Required
from app import db, app
from models import Employee
from flask.ext.babel import gettext 
import re

LocalizedRequired = Required(gettext('This field is required.'))

def make_valid_name(name):
	return name
	

class EmployeeForm(Form):	
	# TODO: write custom validator for name fields - to use ascii only
	employeefirstname = TextField('employeefirstname', validators=[LocalizedRequired])
	employeesurname = TextField('employeesurname', validators=[LocalizedRequired])	
	employeeid = HiddenField('employeeid')

	def __init__(self, formdata=None, obj=None, *args, **kwargs):
		Form.__init__(self, formdata, obj, *args, **kwargs)
		self.employeefirstname.label.text = gettext('employeefirstname')
		self.employeesurname.label.text = gettext('employeesurname')
		if formdata:
			app.logger.info('formdata supplied, we use it')
			self.employeesurname.data = formdata['employeefirstname']
			self.employeesurname.data = formdata['employeesurname']
		elif obj:
			app.logger.info('obj supplied, we use it as employee')
			employee=obj
			self.employeefirstname.data = employee.employeefirstname
			self.employeesurname.data = employee.employeesurname
			self.employeeid.data = employee.employeeid
			
	def validate(self):
		app.logger.info('entering validator')
		if not Form.validate(self):
			app.logger.info('Form.validate(self) failed!')
			return False
		if self.employeefirstname.data != make_valid_name(self.employeefirstname.data):
			self.employeefirstname.errors.append(gettext('This name has invalid characters. Please use letters, numbers, dots and underscores only.'))
			return False
		if self.employeesurname.data != make_valid_name(self.employeesurname.data):
			self.employeesurname.errors.append(gettext('This name has invalid characters. Please use letters, numbers, dots and underscores only.'))
			return False
		app.logger.info('validation passed successfully!')
		return True


			
			
		